package org.seedltd.upms.mapper.detection;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.seedltd.core.base.BasePage;
import org.seedltd.core.base.IBaseMapper;
import org.seedltd.upms.model.detection.entity.DetectionTube;
import org.seedltd.upms.model.detection.form.DetectionTubeSearchForm;
import org.seedltd.upms.model.detection.vo.DetectionTubeVO;

/**
 * 试剂管维护数据库访问接口
 */
public interface DetectionTubeMapper extends IBaseMapper<DetectionTube> {

    @Select("select 1 from detection_order where qrcode_id = #{qrcodeId} and  tube_id = #{tubeId} limit 1")
    Integer checkOrderExists(@Param("qrcodeId")Long qrcodeId, @Param("tubeId") Long tubeId);

    @Select("select id from detection_order where qrcode_id = #{qrcodeId} and  tube_id = #{tubeId} limit 1")
    Long findOrderId(@Param("qrcodeId") Long qrcodeId, @Param("tubeId") Long tubeId);

    /**
     * 修改订单状态为检测完成
     * @param checkState 检测结果状态
     * @param qrcodeId 二维码主键
     * @param tubeId 试剂管主键
     * @return
     */
    @Update("update detection_order set check_state=#{checkState}, state=15 where qrcode_id = #{qrcodeId} and  tube_id = #{tubeId} limit 1")
    int updateOrderState(@Param("checkState")Integer checkState, @Param("qrcodeId")Long qrcodeId,  @Param("tubeId") Long tubeId);

    BasePage<DetectionTubeVO> list(@Param("page") BasePage<DetectionTubeVO> page,
        @Param("ew") DetectionTubeSearchForm searchForm);
    
    DetectionTube getByQrcode(String qrcodeNo);
}
